package com.siysoft.tc.admin.service.Impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.siysoft.tc.admin.service.IAdminService;
import com.siysoft.tc.base.bean.to.Manager;
import com.siysoft.tc.base.bean.to.RoleManager;
import com.siysoft.util.IMap;
import com.tc.base.dao.BaseDAO;

/**
 * 方法摘要：实现后台管理员管理功能接口 作者：zhouyu 日期：2015年6月5日
 * 
 * @param iMap
 * @return
 * @throws Exception
 */

@Service
public class AdminServiceImpl implements IAdminService {
	@Autowired
	BaseDAO baseDao;

	// 管理员修改功能
	@Override
	public int reviseAdmin(IMap iMap) throws Exception {
		String hql = "update Manager set username = '"
				+ iMap.getString("username") + "' where managerId = "
				+ iMap.getInt("manager_id") + " ";
		return (Integer) baseDao.updateByHQL(hql);
	}

	// 管理员查询用户信息
	@Override
	public List<Map<String, String>> seekInfo(IMap iMap) throws Exception {
		String hql = "select manager_id,username from db_manager";
		return baseDao.findBySQLToMap(hql, null, -1, -1);
	}

	// 管理员添加管理功能
	@Override
	public void saveAdmin(Manager manager) throws Exception {
		manager.setEnabled(true);
		manager.setAccountNonExpired(true);
		manager.setAccountNonLocked(true);
		manager.setCredentialsNonExpired(true);
		Manager saveReturn = (Manager) baseDao.saveReturn(manager);
		RoleManager roleManager = new RoleManager();
		roleManager.setManagerId(saveReturn.getManagerId());
		roleManager.setRoleId(4);
		baseDao.save(roleManager);
	}

	// 管理员修改用户信息功能
	@Override
	public List<Map<String, String>> reviseUser(IMap iMap) throws Exception {

		return null;
	}

	// 跳转管理员信息修改页面
	@Override
	public Map<String, String> toReviseAdmin(IMap iMap) throws Exception {
		String hql = "select manager_id,username from db_manager where manager_id = "
				+ iMap.getInt("manager_id") + " ";
		List list = baseDao.findBySQLToMap(hql, iMap, -1, -1);
		if (list.size() > 0) {
			return (Map<String, String>) list.get(0);
		} else {
			return null;
		}
	}
	
	//管理员修改密码
	@Override
	public int reviseAdminPassword(IMap iMap) throws Exception {
		String hql = "update Manager set password = '"+iMap.getString("password")+"' "
				+ "where managerId = "+iMap.getInt("manager_id")+" ";
		return (Integer) baseDao.updateByHQL(hql);
	}
	
	//添加管理员AJAX验证
	@Override
	public void checkName(String username) throws Exception {
		String hql="from Manager where username='"+username+"'";
		List<Manager> managerList=new ArrayList();
		if(username!=null){
			managerList=baseDao.findByHQL(hql);
			if(managerList.size()>0){
				throw new Exception();
			}
		}
		
	}
//查询未审核教师增长率
	@Override
	public List<Map<String, String>> getTeacherCount(IMap iMap) throws Exception {
		String hql="SELECT count(user_id) as num from db_user where user_type = '1' ";
//		String hql="SELECT ROUND(s.new/q.counts*100) as rate,s.new as number from (SELECT count(a.teacher_id) as new from db_teacher a where a.teacher_status=0 ) s ,(SELECT count(a.teacher_id) as counts from db_teacher a) q";
		List<Map<String, String>> dataList = baseDao.findBySQLToMap(hql, null, 0, 1);
		return dataList;
		
	}
	
	//查询待审核的老师
	@Override
	public List<Map<String, String>> getTeacher(IMap iMap) throws Exception {
		String hql="SELECT b.user_id,date_format(b.user_create_time,'%Y-%m-%d %H:%i') as create_time ,b.username,b.user_sex from db_user b where b.user_type=1 ";
//		String hql="SELECT a.user_id,date_format(b.create_time,'%Y-%m-%d %H:%i') as create_time ,a.teacher_name from db_teacher a,db_user b where a.teacher_status=0 and a.user_id=b.user_id";
		List<Map<String, String>> dataList = baseDao.findBySQLToMap(hql, null, 0, 5);
		return dataList;
		
	}
	
	//管理员修改密码
	@Override
	public void managerRevisePassword(Manager manager) throws Exception {
		manager.setEnabled(true);
		manager.setAccountNonExpired(true);
		manager.setAccountNonLocked(true);
		manager.setCredentialsNonExpired(true);
		baseDao.update(manager);
	}
	@Override
	public Manager findManager(IMap iMap){
		
		return (Manager) baseDao.findById(Manager.class, iMap.getInt("manager_id"));
	}

	@Override
	public List<Map<String, String>> getStudentCount(IMap iMap) throws Exception {
		String hql="SELECT  COUNT(t.user_id) AS number FROM db_user t WHERE t.`user_type` = 0";
		if(!Objects.equals("6", iMap.get("channel"))){
			hql = hql+" AND t.channel = "+iMap.get("channel");
		}
		List<Map<String, String>> dataList = baseDao.findBySQLToMap(hql, null, 0, 1);
		return dataList;
	}
}
